float height=50;
JSONObject accounts;
void setup(){
  size(1500,1000);
  background(0);
  stroke(255);
  noLoop();

  accounts = loadJSONObject("data/new.json");
}
void draw(){
  drawTree(accounts,0,50,0);
}
void drawTree(JSONObject object,int level,float px,float py){
  float cy=0;
  float cx=px+200;
  
  String name=object.getString("name");
  JSONArray children=object.getJSONArray("children");
  if(children.size()==0){
    cy=height+50*descendants(object);
    height=cy+50;
    
  }else{
    cy=height+25*descendants(object)-25;
  }
  text(name,cx,cy);
  if(level!=0){
    ellipse(px+100,py-5,2,2);
    ellipse(cx-10,cy-5,2,2);
    line(px+100,py-5,cx-10,cy-5);
  }
  println(name+"-"+cy+"-"+height);
  for(int i=0;i<children.size();i++){
    drawTree(children.getJSONObject(i),level+1,cx,cy);
  }
  save("accounts.png");
}

int descendants(JSONObject object){
  int cnt=0;
  JSONArray children=object.getJSONArray("children");
  for(int i=0;i<children.size();i++){
    JSONArray clds=children.getJSONObject(i).getJSONArray("children");
    if(clds.size()==0) {
      cnt++;
    }else{
      cnt+=descendants(children.getJSONObject(i));
    }
  }
  return cnt;
}
